/*******************************************************************************
 *  ButtonElement class
 *******************************************************************************

// options example
var options = {
    id: 'id',
    classes: 'classes',
    title: 'title',
    primary:  true,
    disabled: false,
    icon: 'icon-user',
    hint: 'hint',
    onclick: function(){}
}

*/

var ButtonElement = function(options) {
    
    // this class should extend an Element class
    $.extend(true, this, new Element($('<button>')))
    this.getElement()[0].wrapper = this;
    // ------------------------------------------------------- settings
    this.defaults = {
        id: ''
    };
    
    var settings = this.prepareSettings(options);
    // ------------------------------------------------------- methods
    
    /**
     *  Sets button title
     *  @param value
     *  @return class instance
     */
    this.setTitle = function(value){
        this.setText(value);
        return this;
    }
    
    this.isPrimary = function(){
        return this.settings.primary;
    }
    
    /**
     *  Sets button state
     *  @param isEnabled defines button state
     *  @return class instance
     */
    this.setEnabled = function(isEnabled){
        var disabled = 'disabled';
        if(isEnabled) {
            this.removeClass(disabled);
        } else {
            this.addClass(disabled);
        }
        return this;
    }
    
    // ------------------------------------------------------- element creation
    this.setId(settings.id)
        .addClass(settings.classes)
        .setTitle(settings.title==null ? '' : (settings.icon ? ' '+settings.title : settings.title))
        .addAttribute('title', settings.hint==null ? (settings.title ? settings.title : '') : settings.hint)
        .addAttribute('type', 'button');
        
    if(settings.disabled) this.disable();
        
    if(settings.icon){
        $('<i>').addClass(settings.icon+(settings.primary ? ' icon-white' : '')).prependTo(this.getElement());
    }
        
    if(settings.onclick) {
        var handler = settings.onclick;
        this.getElement()[0].onclick = function(e){
            if(this.wrapper.isDisabled()) return;
            handler(e);
        };
    }
    
    // customize
    this.addClass('btn');
    if(settings.primary) this.addClass('btn-primary');
    
    console.log('ButtonElement was created with id = '+this.getId());
}